# -*- coding: utf-8 -*-
import pymysql
from Logs.log import log1


# 数据库配置
MYSQL_HOST = "127.0.0.1"
MYSQL_DBNAME = "eco_shopee_goods"
MYSQL_USER = "DATAGRAB"
MYSQL_PASSWORD = "PcuPMSOsKROx5995"
MYSQL_PORT = 3306

# 数据连接
connect = pymysql.connect(
    host=MYSQL_HOST,
    db=MYSQL_DBNAME,
    user=MYSQL_USER,
    passwd=MYSQL_PASSWORD,
    port=MYSQL_PORT,
    charset='utf8',
    use_unicode=True)
# cursor = connect.cursor()


# 获取企业名称
def get_names():
    sql = """
        select STOREID from SHOPEE_STORELIST
    """
    try:
        cursor = connect.cursor()
        cursor.execute(sql)
        usernames = cursor.fetchall()
        cursor.close()
    except:
        log1.error("获取SHOPEE_STORELIST数据失败")
        return []
    return usernames


# 数据保存
def save_data(connect, datas):
    sql = "insert into SHOPEE_GOODS(ID, DATE, STORENAME, GOODSNAME, SCORE, COMMENTS, SOLD, PRICE, STORAGE, FIVESTARS, FOURSTARS, THREESTARS, TWOSTARS, ONESTAR, NOTECOMMENTS) values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
        datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10],
        datas[11], datas[12], datas[13], datas[14])
    cursor = connect.cursor()
    try:
        cursor.execute(sql)
        connect.commit()
        log1.info("{}数据保存成功".format(datas[0]))
    except Exception as e:
        # print("{}数据保存失败:{}".format(datas[0], e))
        log1.error("{}数据保存失败:{}".format(datas[0], e))
    cursor.close()
